﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace mysix
{
    public partial class Form1 : Form
    {
        public string ValueToPass { get; set; }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label6.ForeColor = label6.BackColor;
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowOnly;
            dbmanager mydb = new dbmanager();
            DataTable schoolData1 = mydb.LoadLogsData();
            dataGridView2.DataSource = schoolData1;
            
            dataGridView2.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridView2.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
          
            comboBox1.Items.Add("学校管理");
            comboBox1.Items.Add("班级管理");
            comboBox1.Items.Add("学生管理");
            comboBox1.SelectedIndex = 0;
            SchoolDB school = new SchoolDB();

            // 创建一个 DataTable 以显示 School 表的数据
            DataTable schoolData = school.GetSchoolData();

            // 创建一个 DataGridView 控件并设置数据源
           
            dataGridView1.DataSource = schoolData;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text =="学校管理")
            {
                SchoolDB myschool = new SchoolDB();
                ValueToPass = "Add";
                Form2 popupForm = new Form2(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myschool.GetSchoolData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();

                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "班级管理")
            {
                ClassDB myclass = new ClassDB();
                ValueToPass = "Add";
                Form3 popupForm = new Form3(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myclass.GetClassData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();
               
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "学生管理")
            {
                StuDB mystu = new StuDB();
                ValueToPass = "Add";
                Form4 popupForm = new Form4(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = mystu.GetStudentData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();

                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "学校管理")
            {
                string s = textBox1.Text;
                SchoolDB myschool = new SchoolDB();
                myschool.DeleteSchool(int.Parse(s));
                DataTable schoolData = myschool.GetSchoolData();
                dbmanager mydb = new dbmanager();
                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                mydb.AddLog("School", "Delete", "删除了SchoolId为" + s + "的学校");
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "班级管理")
            {
                ClassDB myclass = new ClassDB();
                string s1 = textBox1.Text;
                    string s2 = textBox2.Text;
                myclass.DeleteClass(int.Parse(s1), int.Parse(s2));

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myclass.GetClassData();
                dbmanager mydb = new dbmanager();
               
                mydb.AddLog("Class", "Delete", "删除了ClassId为" + s2 + "的班级");
                dataGridView1.DataSource = schoolData;
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "学生管理")
            {
                StuDB mystu = new StuDB();
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                string s3 = textBox3.Text;
                mystu.DeleteStudent(int.Parse(s1), int.Parse(s2),int.Parse(s3));

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = mystu.GetStudentData();

                // 创建一个 DataGridView 控件并设置数据源
                dbmanager mydb = new dbmanager();

                mydb.AddLog("Student", "Delete", "删除了StuId为" + s3 + "的学生");
                dataGridView1.DataSource = schoolData;
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "学校管理")
            {
                SchoolDB myschool = new SchoolDB();
                ValueToPass = "Update";
                Form2 popupForm = new Form2(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myschool.GetSchoolData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();

                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "班级管理")
            {
                ClassDB myclass = new ClassDB();
                ValueToPass = "Update";
                Form3 popupForm = new Form3(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myclass.GetClassData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();

                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "学生管理")
            {
                StuDB mystu = new StuDB();
                ValueToPass = "Update";
                Form4 popupForm = new Form4(ValueToPass);
                popupForm.ShowDialog();


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = mystu.GetStudentData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();

                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "学校管理")
            {
                string s = textBox1.Text;
                SchoolDB myschool = new SchoolDB();

                DataTable schoolData = myschool.Search1School(int.Parse(s));

                // 创建一个 DataGridView 控件并设置数据源
               
                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();
                mydb.AddLog("School", "Search", "查询了SchoolIdd为" + s + "的学校");
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "班级管理")
            {
                ClassDB myclass = new ClassDB();
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
               

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myclass.Search1Class(int.Parse(s1), int.Parse(s2));
                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();
                mydb.AddLog("Class", "Search", "查询了ClassId为" + s2 + "的班级");
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
            if (comboBox1.Text == "学生管理")
            {
                StuDB mystu = new StuDB();
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                string s3 = textBox3.Text;


                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = mystu.Search1Student(int.Parse(s1), int.Parse(s2), int.Parse(s3));
                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
                dbmanager mydb = new dbmanager();
                mydb.AddLog("Student", "Search", "查询了StuId为" + s3 + "的学生");
                DataTable schoolData1 = mydb.LoadLogsData();
                dataGridView2.DataSource = schoolData1;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "学校管理")
            {
                SchoolDB myschool = new SchoolDB();
             

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myschool.GetSchoolData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
            }
            if (comboBox1.Text == "班级管理")
            {
                ClassDB myclass = new ClassDB();
              

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = myclass.GetClassData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
            }
            if (comboBox1.Text == "学生管理")
            {
                StuDB mystu = new StuDB();
                

                // 创建一个 DataTable 以显示 School 表的数据
                DataTable schoolData = mystu.GetStudentData();

                // 创建一个 DataGridView 控件并设置数据源

                dataGridView1.DataSource = schoolData;
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
